package com.jack.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.feign.FeignClientsConfiguration;
import org.springframework.context.annotation.Import;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import com.jack.entity.User;
import com.jack.service.UserFeignClient;

import feign.Client;
import feign.Contract;
import feign.Feign;
import feign.auth.BasicAuthRequestInterceptor;
import feign.codec.Decoder;
import feign.codec.Encoder;

@RestController
@Import(FeignClientsConfiguration.class)
public class UserMovieController {

	//NOTE: there is not autowired annotation
	private UserFeignClient userUserFeignClient;

	private UserFeignClient adminUserFeignClient;
	
	@Autowired
	public UserMovieController(Decoder decoder, Encoder encoder, Client client, Contract contract){
		this.userUserFeignClient = Feign.builder().client(client).contract(contract).decoder(decoder).encoder(encoder)
		.requestInterceptor(new BasicAuthRequestInterceptor("user", "password1"))
		.target(UserFeignClient.class, "http://microservice-provider-user");
		
		this.adminUserFeignClient = Feign.builder().client(client).contract(contract).decoder(decoder).encoder(encoder)
		.requestInterceptor(new BasicAuthRequestInterceptor("admin", "password2"))
		.target(UserFeignClient.class, "http://microservice-provider-user");
	}
	
	
	@GetMapping("/user-user/{id}")
	public User findOneByUser(@PathVariable Long id){
		User user =userUserFeignClient.findById(id);
		return user;
	}
	
	@GetMapping("/user-admin/{id}")
	public User findOneByAdmin(@PathVariable Long id){
		User user =adminUserFeignClient.findById(id);
		return user;
	}
	
	
}
